package com.android.nfctouch.ui.activity;

import android.view.View;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.android.baselibrary.interface_.OkHttpCallBack;
import com.android.baselibrary.responce.BaseResponce;
import com.android.baselibrary.tool.CommToast;
import com.android.baselibrary.view.Comm_HeadView;
import com.android.baselibrary.view.MultiStateView;
import com.android.nfctouch.R;
import com.android.nfctouch.adapter.A_ModelAdapter;
import com.android.nfctouch.constants.Constants;
import com.android.nfctouch.manager.API_Manager;
import com.android.nfctouch.responce.BaseListData;
import com.scwang.smart.refresh.layout.SmartRefreshLayout;
import com.scwang.smart.refresh.layout.api.RefreshLayout;
import com.scwang.smart.refresh.layout.listener.OnLoadMoreListener;
import com.scwang.smart.refresh.layout.listener.OnRefreshListener;

import butterknife.BindView;

public class A_Model_ListActivity extends BaseActivity {

    @BindView(R.id.multiplestatusView)
    MultiStateView multiplestatusView;
    @BindView(R.id.recyclerview)
    RecyclerView recyclerview;
    @BindView(R.id.smartrefreshlayout)
    SmartRefreshLayout smartrefreshlayout;
    @BindView(R.id.comm_title)
    Comm_HeadView comm_title;

    A_ModelAdapter adapter;
    int page = 1;
    int size = 20;
    @Override
    protected int getLayoutId() {
        return R.layout.activity_onlylist;
    }

    @Override
    protected void initView() {
        comm_title.setTitle("页面标题");
        multiplestatusView.setOnRetryListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                multiplestatusView.setViewState(MultiStateView.ViewState.LOADING);
                getData();
            }
        });

        recyclerview.setLayoutManager(new LinearLayoutManager(mContext));
        adapter = new A_ModelAdapter(mContext, null);
        recyclerview.setAdapter(adapter);

        smartrefreshlayout.setOnRefreshListener(new OnRefreshListener() {
            @Override
            public void onRefresh(@NonNull RefreshLayout refreshLayout) {
                refreshData();
            }
        });
        smartrefreshlayout.setOnLoadMoreListener(new OnLoadMoreListener() {
            @Override
            public void onLoadMore(@NonNull RefreshLayout refreshLayout) {
                loadMoreData();
            }
        });
    }

    @Override
    protected void initData() {
        getData();
    }

    //下拉刷新
    private void refreshData() {
        size = 20;
        getData();
    }

    //上拉加载
    private void loadMoreData() {
        size += 20;
        getData();
    }

    //获取数据
    private void getData() {
        API_Manager.sendcode(mContext, "", new OkHttpCallBack<BaseResponce<BaseListData<Object>>>() {
            @Override
            public void onSuccess(BaseResponce<BaseListData<Object>> baseResponce) {
                smartrefreshlayout.finishRefresh();
                smartrefreshlayout.finishLoadMore();
                adapter.setData(baseResponce.getData().list);
                if (adapter.getItemCount() == 0) {
                    multiplestatusView.setViewState(MultiStateView.ViewState.EMPTY);
                } else {
                    multiplestatusView.setViewState(MultiStateView.ViewState.CONTENT);
                }
            }

            @Override
            public void onFailure(BaseResponce<BaseListData<Object>> baseResponce) {
                smartrefreshlayout.finishRefresh();
                smartrefreshlayout.finishLoadMore();
                multiplestatusView.setViewState(MultiStateView.ViewState.ERROR);
                CommToast.showToast(mContext, Constants.NetError);
            }
        });
    }

    //点击事件
    //    @OnClick({R.id.tv_sendcode})
//    public void onViewClicked(View view) {
//        switch (view.getId()) {
//            case R.id.tv_sendcode:
//                break;
//        }
//    }
}
